package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.core.interfaces.ActivityService;
import com.yt.app.core.interfaces.ActivityUserService;
import com.yt.app.core.interfaces.AppBannerService;
import com.yt.app.core.model.Activity;
import com.yt.app.core.model.ActivityUser;
import com.yt.app.core.model.AppBanner;
import com.yt.app.core.utils.Constant;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Api(tags = "04.活动")
@RequestMapping("/activity")
public class ActivityController extends BaseAction{
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityUserService activityUserService;

    @ApiOperation(value="活动列表", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "活动属性", response = Activity.class)})
    @RequestMapping("/list")
    public @ResponseBody  Object activityList(
            @ApiParam("上次最后一个ID,传递接口json中返回的lastId") @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId
    ){
        MsgBean msgBean = new MsgBean();
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter("Q_status_N_EQ", "0");
        if(lastId > 0){
            filter.addFilter("Q_id_L_LT", String.valueOf(lastId));
        }
        filter.setSortField("id", true);
        List<Activity> activityList = activityService.find(filter);

        if(activityList != null && !activityList.isEmpty()){
            msgBean.put("lastId", activityList.get(activityList.size()-1).getId());
        }else{
            msgBean.put("lastId", -1);
        }

        msgBean.put("activities", activityList);

        return msgBean.returnMsg();
    }

    @ApiOperation(value="参加活动", httpMethod = "POST")
    @RequestMapping("/join")
    public @ResponseBody  Object join(
            @ApiParam("活动ID") @RequestParam(value = "activityId", required = true) Long activityId,
            @ApiIgnore HttpServletRequest request
            ){
        MsgBean msgBean = new MsgBean();
        Activity activity = activityService.get(activityId);
        if(activity == null){
            return msgBean.failure("活动不存在!").returnMsg();
        }
        ActivityUser activityUser = new ActivityUser();
        activityUser.setActivityId(activityId);
        activityUser.setUid(BaseVo.getUIDFromHeader(request));
        activityUser.setJoinTime(System.currentTimeMillis()/1000);
        activityUser.setStatus(1);
        activityUserService.insertUser(activityUser);
        return msgBean.returnMsg();
    }
}
